<?php
/**
 * Description: 获取平台的用户信息
 * User: Administrator
 * Date: 2017-08-10
 * Time: 14:41
 */
namespace app\wxbackend\model;
use think\Model;

class Member extends Model
{
    private $_wxMember = 'z_wx_member';
    private $_member   = 'z_member';
    private $_relationTable = 'z_relation';

    /**
     * @comment 根据微信openid获取用户信息
     * @author wuhao
     * @param $openid  微信openid
     * @return bool
     */
    public function getMemberByOpenid($openid)
    {
        if (!$openid) {
            return false;
        }

        $where = ['openid' => $openid];
        return $this->table($this->_wxMember)->where($where)->find();
    }

    /**
     * @comment 根据登录账号返回用户信息
     * @author wuhao
     * @param $account
     * @return bool
     */
    public function getMemberByAccount($account)
    {
        if (!$account) {
            return false;
        }

        $where = ['username' => $account];
        return $this->table($this->_member)->where($where)->find();
    }

    /**
     * @comment 根据手机号获取微信绑定信息
     * @author wuhao
     * @param $mobile
     * @return bool
     */
    public function getMemberByMobile($mobile)
    {
        if (!$mobile) {
            return false;
        }

        $where = ['mobile' => $mobile];
        return $this->table($this->_wxMember)->where($where)->find();
    }

    /**
     * @comment 根据手机号获取用户信息
     * @author wuhao
     * @param $mobile
     * @return bool
     */
    public function getMemberInfoByMobile($mobile)
    {
        if (!$mobile) {
            return false;
        }

        $where = ['mobile' => $mobile];
        return $this->table($this->_member)->where($where)->find();
    }

    /**
     * @comment 新增微信关联信息
     * @author wuhao
     * @param $data
     * @return bool
     */
    public function addWxInfo($data)
    {
        if (!$data) {
            return false;
        }

        return $this->table($this->_wxMember)->insertGetId($data);
    }

    /**
     * @comment 更新用户信息
     * @author wuhao
     * @param $where
     * @param $data
     */
    public function updateInfo($where, $data)
    {
        if (!$where || !$data) {
            return false;
        }
        return $this->table($this->_wxMember)->where($where)->update($data);
    }

    /**
     * @comment  删除关联信息
     * @author wuhao
     * @param $openid
     * @return bool
     */
    public function deleteInfo($openid)
    {
        if (!$openid) {
            return false;
        }
        return $this->table($this->_wxMember)->where(['openid' => $openid])->delete();
    }

    /**
     * 查询员工的父账号id
     * @param $mid 员工id
     * @return array
     */
    public function getParentId($mid)
    {
        if (!$mid) {
            return false;
        }
        $where = [
            'son_id' => $mid,
            'son_id_type' => 1,
        ];
        $field = 'parent_id';
        return $this->table($this->_relationTable)->field($field)->where($where)->find();
    }

    public function getMemberInfoById($id)
    {
        if (!$id) {
            return false;
        }
        $where = ['id' => $id];
        return $this->table($this->_member)->where($where)->find();
    }
}